package gui;

import java.awt.AWTException;
import java.awt.Robot;
import java.awt.event.KeyEvent;

import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Image;
import org.newdawn.slick.Input;
import org.newdawn.slick.Music;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.Sound;
import org.newdawn.slick.state.BasicGameState;
import org.newdawn.slick.state.StateBasedGame;

import voix.Voix;
import wiimote.WiimoteLink;
import wiiusej.wiiusejevents.physicalevents.ExpansionEvent;
import wiiusej.wiiusejevents.physicalevents.IREvent;
import wiiusej.wiiusejevents.physicalevents.MotionSensingEvent;
import wiiusej.wiiusejevents.physicalevents.WiimoteButtonsEvent;
import wiiusej.wiiusejevents.utils.WiimoteListener;
import wiiusej.wiiusejevents.wiiuseapievents.ClassicControllerInsertedEvent;
import wiiusej.wiiusejevents.wiiuseapievents.ClassicControllerRemovedEvent;
import wiiusej.wiiusejevents.wiiuseapievents.DisconnectionEvent;
import wiiusej.wiiusejevents.wiiuseapievents.GuitarHeroInsertedEvent;
import wiiusej.wiiusejevents.wiiuseapievents.GuitarHeroRemovedEvent;
import wiiusej.wiiusejevents.wiiuseapievents.NunchukInsertedEvent;
import wiiusej.wiiusejevents.wiiuseapievents.NunchukRemovedEvent;
import wiiusej.wiiusejevents.wiiuseapievents.StatusEvent;

import constantes.Constantes;

public class MainMenuState extends BasicGameState implements WiimoteListener{
	Image background = null;
	Image startDRAGGameOption = null;
	Image startESPIONOption = null;
	Image exitOption = null;
	Image startDRAGGameOptionj = null;
	Image startESPIONOptionj = null;
	Image exitOptionj = null;
	Sound fx = null;
	Music intro;
	float scaleStep = 0.00001f;
	float startDRAGGameScale = 1;
	float startESPIONGameScale = 1;
	float exitScale = 1;
	int stateID = -1;
	private static int menuX1 = 380;
	private static int menuX2 = 380;
	private static int menuX3 = 380;
	private static int menuY1 = 200;
	private static int menuY2 = 350;
	private static int menuY3 = 510;
	boolean insideStartGame = false;
	boolean insideExit = false;
	boolean insideEspion = false;
	Robot robot;

	MainMenuState(int stateID) {
		this.stateID = stateID;
		try {
			robot = new Robot();
		} catch (AWTException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	@Override
	public int getID() {
		return stateID;
	}

	@Override
	public void enter(GameContainer gc, StateBasedGame sb)
			throws SlickException {
		super.enter(gc, sb);
		intro.play(1.0f, 0.5f);
		//Voix.read("Menu principal");

	}

	@Override
	public void leave(GameContainer gc, StateBasedGame sb)
			throws SlickException {
		super.enter(gc, sb);
		fx.stop();

	}

	public void init(GameContainer gc, StateBasedGame sbg)
			throws SlickException {
		background = new Image("../Images/castv1.jpg");

		startDRAGGameOptionj = new Image("../Images/cast1.jpg");
		startESPIONOptionj = new Image("../Images/cast2.jpg");
		exitOptionj = new Image("../Images/cast3.jpg");

		fx = new Sound("../sounds/menu.ogg");
		intro = new Music("../sounds/Introduction.ogg");
		WiimoteLink.addWiiRemoteListener(this);

	}

	public void render(GameContainer gc, StateBasedGame sbg, Graphics g)
			throws SlickException {
		// render the background
		background.draw(0, 0, Constantes.resolutionX, Constantes.resolutionY);

		// Draw menu
		if (insideStartGame) {
			startDRAGGameOptionj.draw(0, 0, Constantes.resolutionX,
					Constantes.resolutionY);

		}

		if (insideEspion)
			startESPIONOptionj.draw(0, 0, Constantes.resolutionX,
					Constantes.resolutionY);

		if (insideExit)
			exitOptionj.draw(0, 0, Constantes.resolutionX,
					Constantes.resolutionY);

	}

	public void update(GameContainer gc, StateBasedGame sbg, int delta)
			throws SlickException {
		if (!intro.playing() && !fx.playing())
			fx.loop(1.0f, 0.2f);
		Input input = gc.getInput();
		
		if (Constantes.mouseenable) {
			int mouseX = input.getMouseX();
			int mouseY = input.getMouseY();

			insideStartGame = false;
			insideExit = false;
			insideEspion = false;

			if ((mouseX >= menuX1 && mouseX <= menuX1 + 270)
					&& (mouseY >= menuY1 && mouseY <= menuY1 + 55)) {
				insideStartGame = true;
			} else if ((mouseX >= menuX2 && mouseX <= menuX2 + 270)
					&& (mouseY >= menuY2 && mouseY <= menuY2 + 55)) {
				insideEspion = true;
			} else if ((mouseX >= menuX3 && mouseX <= menuX3 + 270)
					&& (mouseY >= menuY3 && mouseY <= menuY3 + 55)) {
				insideExit = true;
			}

			if (insideStartGame) {

				// startDRAGGameScale += scaleStep * delta;

				if (input.isMouseButtonDown(Input.MOUSE_LEFT_BUTTON)) {

					sbg.enterState(SlickCastleDefense.GAMEPLAYSTATE);

				}
			} else {
				if (startDRAGGameScale > 1.0f)
					startDRAGGameScale -= scaleStep * delta;

				if (input.isMouseButtonDown(Input.MOUSE_LEFT_BUTTON))

					gc.exit();
			}

		} else {
			if (input.isKeyPressed(Input.KEY_DOWN)) {
				intro.stop();
				if (insideStartGame) {
					insideStartGame = false;
					insideEspion = true;
					Voix.read("Options");
				}

				else {
					if (insideEspion) {
						insideEspion = false;
						insideExit = true;
						Voix.read("Quitter");
					} else {
						if (insideExit) {
							insideExit = false;
							insideStartGame = true;
							Voix.read("Jouer");
						} else{
							insideStartGame = true;
							Voix.read("JOUER");
						}
					}
				}
			}

			if (input.isKeyPressed(Input.KEY_UP)) {
				intro.stop();
				if (insideStartGame) {
					insideStartGame = false;
					insideExit = true;
					Voix.read("Quitter");
				}

				else {
					if (insideEspion) {
						insideEspion = false;
						insideStartGame = true;
						Voix.read("Jouer");
					} else {
						if (insideExit) {
							insideExit = false;
							insideEspion = true;
							Voix.read("Options");
						} else{
							insideStartGame = true;
						Voix.read("Jouer");
						}
					}
				}
			}
			if (input.isKeyPressed(Input.KEY_ENTER)) {
				intro.stop();
				if (insideStartGame) {
					sbg.enterState(SlickCastleDefense.GAMEPLAYSTATE);
				}
				else {
					if (insideEspion) {
						sbg.enterState(SlickCastleDefense.OPTIONSSTATE);
					} else {
						if (insideExit) {
							gc.exit();
						}
					}

				}
			}
				if (input.isKeyPressed(Input.KEY_F11))
					gc.setFullscreen(!gc.isFullscreen());
				 

				if (input.isKeyPressed(Input.KEY_S)){
					System.out.print("Music previously : " + fx.playing());
					if (fx.playing())
						fx.stop();
					else
						fx.loop(1.0f,0.3f);
					System.out.println(" Now : " + fx.playing());
				}
				if (input.isKeyPressed(Input.KEY_H)){
					if (fx.playing()){
						fx.stop();
						intro.play(1.0f, 0.5f);
					}
				}
				
				if (input.isKeyDown(Input.KEY_ESCAPE))
					gc.exit();
				
				WiimoteLink.removeWiiRemoteListener(this);
			
		}
	}

	@Override
	public void onButtonsEvent(WiimoteButtonsEvent arg0) {
		if (robot != null){
			if (arg0.isButtonUpPressed()) {
				robot.keyPress(KeyEvent.VK_UP);
				robot.keyRelease(KeyEvent.VK_UP);
			}
			if (arg0.isButtonDownPressed()) {
				robot.keyPress(KeyEvent.VK_DOWN);
				robot.keyRelease(KeyEvent.VK_DOWN);
			}
			if (arg0.isButtonAPressed()) {
				robot.keyPress(KeyEvent.VK_ENTER);
				robot.keyRelease(KeyEvent.VK_ENTER);
			}
		}
	}

	@Override
	public void onClassicControllerInsertedEvent(
			ClassicControllerInsertedEvent arg0) {

	}

	@Override
	public void onClassicControllerRemovedEvent(
			ClassicControllerRemovedEvent arg0) {

	}

	@Override
	public void onDisconnectionEvent(DisconnectionEvent arg0) {

	}

	@Override
	public void onExpansionEvent(ExpansionEvent arg0) {

	}

	@Override
	public void onGuitarHeroInsertedEvent(GuitarHeroInsertedEvent arg0) {

	}

	@Override
	public void onGuitarHeroRemovedEvent(GuitarHeroRemovedEvent arg0) {

	}

	@Override
	public void onIrEvent(IREvent arg0) {

	}

	@Override
	public void onMotionSensingEvent(MotionSensingEvent arg0) {

	}

	@Override
	public void onNunchukInsertedEvent(NunchukInsertedEvent arg0) {

	}

	@Override
	public void onNunchukRemovedEvent(NunchukRemovedEvent arg0) {

	}

	@Override
	public void onStatusEvent(StatusEvent arg0) {

	}
}

